package com.sigmatrix.h5.web.controller;

import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.sigmatrix.h5.dto.DisTerminalShopData;
import com.sigmatrix.h5.enums.ResponseEnum;
import com.sigmatrix.h5.request.Request;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.DisTerminalShopService;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.web.controller.common.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api(tags="终端门店相关接口")
@RestController
@RequestMapping("/app/terminalStore")
public class TerminalStoreController extends BaseController {

	/**logger record*/
	private static final Logger LOGGER = LoggerFactory.getLogger(TerminalStoreController.class);
	
	/**终端门店业务层接口*/
	@Autowired
	private transient DisTerminalShopService disTerminalShopService;
	
	@ApiOperation(value="根据经销商id获取终端门店信息")
	@PostMapping(value="/getById")
	public Response findById(@Valid @RequestBody final Request requestVo,
			final BindingResult result){
		LOGGER.info("根据经销商id获取终端门店信息接口请求参数={}",JSON.toJSONString(requestVo));
		try {
			if (result.hasErrors()) {
				final String errInfo = result.getAllErrors().get(0).getDefaultMessage();
				LOGGER.error("根据经销商id获取终端门店信息接口参数验证失败{}", errInfo);
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, errInfo);
			}
			List<DisTerminalShopData> list = disTerminalShopService.findByDealerId(requestVo.getUserDealerId(), requestVo.getEnterpriseCode());
			return new Response(ResponseEnum.SUCCESS, list);
		} catch (Exception e) {
			LOGGER.error("根据经销商id获取终端门店信息接口失败", e);
			return new Response(ResponseEnum.EXCEPTION);
		}
	}
}
